package com.cq.oa.web.action;

import javax.servlet.http.HttpSession;

import oa.base.session.SessionManger;
import oa.base.web.Action.BaseAction;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.cq.oa.domain.sm.User;
import com.cq.oa.service.sm.UserService;
/**
 * 登录 action
 * @author lilang
 *
 */
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Component
public class LoginAction extends BaseAction<User>{

	
	private String username;
	private String password;
	private String keyword;
	private String randCode;
	
	public String getRandCode() {
		return randCode;
	}

	public void setRandCode(String randCode) {
		this.randCode = randCode;
	}

	private UserService userService;
	
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1673048743924724611L;
	
	/**
	 * 登录
	 * @return
	 */
	public String login(){
		System.out.println("欢迎进入");
		try{
			//checkRand();
			userService.doCheckAccount(username,password,keyword);
		}catch(Exception e){
			System.out.println(e.getMessage());
			SessionManger.setErrorMsg(ServletActionContext.getRequest().getSession(),e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	/**
	 * 登出
	 * @return
	 */
	public String logout(){
		SessionManger.removeUserInfo(ServletActionContext.getRequest().getSession());
		return SUCCESS;
	}
	
	/**
	 * 登出
	 * @return
	 */
	public String findUser(){
		return this.renderJsonSuccess("lilang");
	}

	@Override
	protected Class<User> getEntityClass() {
		return User.class;
	}
	
	public void checkRand() throws Exception{
		HttpSession httpSession=ServletActionContext.getRequest().getSession(true);
		String code = (String) httpSession.getAttribute("checkRand");
		if(randCode==null||!randCode.equals(code)){
			throw new Exception("验证码输入错误");
		}
	}

}
